<template>
  <div class="image-generator-example">
    <h3>图片生成示例</h3>
    
    <div class="image-preview-container">
      <div v-if="selectedImageUrl" class="image-preview">
        <img :src="selectedImageUrl" alt="生成的图片" class="preview-image" />
      </div>
      <div v-else class="empty-preview">
        <el-icon><Picture /></el-icon>
        <span>暂无图片</span>
      </div>
    </div>
    
    <div class="action-buttons">
      <el-button color="#1c1d29" @click="openPreGenerateDialog">
        生成图片
      </el-button>
      <el-button v-if="selectedImageUrl" @click="clearImage">
        清除图片
      </el-button>
    </div>
    
    <!-- 预生成弹窗 -->
    <PreGenerateDialog
      v-model:visible="dialogVisible"
      :initialDescription="initialDescription"
      @confirm="handleImageSelected"
      @cancel="handleDialogCancel"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Picture } from '@element-plus/icons-vue'
import PreGenerateDialog from '@/components/common/PreGenerateDialog.vue'

// 弹窗显示状态
const dialogVisible = ref(false)

// 选中的图片URL
const selectedImageUrl = ref('')

// 初始描述文本
const initialDescription = ref('一个美丽的风景')

// 打开预生成弹窗
const openPreGenerateDialog = () => {
  dialogVisible.value = true
}

// 处理图片选择
const handleImageSelected = (imageUrl) => {
  if (imageUrl) {
    selectedImageUrl.value = imageUrl
    ElMessage.success('图片已选择')
  }
}

// 处理弹窗取消
const handleDialogCancel = () => {
  ElMessage.info('已取消选择')
}

// 清除图片
const clearImage = () => {
  selectedImageUrl.value = ''
  ElMessage.success('图片已清除')
}
</script>

<style scoped>
.image-generator-example {
  padding: 20px;
}

.image-preview-container {
  margin: 20px 0;
  width: 100%;
  height: 300px;
  border: 1px dashed #dcdfe6;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.image-preview {
  width: 100%;
  height: 100%;
}

.preview-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.empty-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #909399;
}

.empty-preview .el-icon {
  font-size: 48px;
  margin-bottom: 10px;
}

.action-buttons {
  display: flex;
  gap: 10px;
}
</style> 